package com.ncp.model.biz.index;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.ncp.model.dao.DbConnection;
import com.ncp.model.entity.index.NaviBean;

public class NaviManager {
	private Connection ct=null;
	private PreparedStatement ps=null;
	private ResultSet rs=null;
	//从数据库取出所有导航菜单
	public ArrayList getAllNavi(){
		ArrayList al=new ArrayList();
		try {
			ct = DbConnection.getConnByDataSource();
			String sql="select * from NAVI";
			ps=ct.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				NaviBean nb=new NaviBean();
				nb.setId(rs.getInt(1));
				nb.setName(rs.getString(2));
				nb.setUrlpage(rs.getString(3));
				al.add(nb);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return al;
	}
	//根据id从数据库取出导航菜单
	public NaviBean getNavi(String id){
		NaviBean nb=new NaviBean();
		try {
			ct = DbConnection.getConnByDataSource();
			String sql="select * from NAVI where NAVIID="+id;
			ps=ct.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()){
				nb.setId(rs.getInt(1));
				nb.setName(rs.getString(2));
				nb.setUrlpage(rs.getString(3));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return nb;
	}
	//修改菜单
	public boolean updateNavi(String id,String name,String urlpage){
		boolean b=false;
		try{
			ct = DbConnection.getConnByDataSource();
			String sql="update NAVI set naviname='"+name+"',urlpage='"+urlpage+"' where NAVIID="+id;
			ps=ct.prepareStatement(sql);
			int num=ps.executeUpdate();
			if(num==1){
				//删除成功！
				b=true;
			}
		}catch(Exception e){
			e.printStackTrace();	
		}finally{
			this.closeResource();
		}
		return b;
	}
	//向数据库插入导航菜单
	public boolean addNavi(String name,String urlpage){
		boolean b=false;
		try {
			ct = DbConnection.getConnByDataSource();
			String sql="insert into NAVI values(null,'"+name+"','"+urlpage+"')";
			ps=ct.prepareStatement(sql);
			ps.executeUpdate();
			b=true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return b;
	}
	//根据id删除导航菜单
	public boolean delNavi(String id){
		Boolean b=false;
		try{
			ct = DbConnection.getConnByDataSource();
			String sql="delete from NAVI where NAVIID='"+id+"'";
			ps=ct.prepareStatement(sql);
			int num=ps.executeUpdate();
			if(num==1){
				//删除成功！
				b=true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return b;
	}

	//关闭资源
	public void closeResource(){
		try {
			if(rs!=null){
				rs.close();
				rs=null;
			}
			if(ps!=null){
				ps.close();
				ps=null;
			}
			if(ct!=null){
				ct.close();
				ct=null;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}
